package com.example.anni.zhuzhoujia.tianya12345;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.anni.zhuzhoujia.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangshengwen2016 on 2016/6/13.
 * 全部爆料
 */
public class BrokeFragmentWhole extends Fragment {
    public ListView list_item;
    public List<Map<String, Object>> list;
    public JSONArray data;
    public String user;//个人信息
    public String avatar;//头像
    public String nickName;//昵称
    public String dateline;//爆料时间
    public String title;//标题
    public String summary;//摘要
    public String good;//赞
    public String bad;//踩
    public String commentNum;//评论数
    public String images;//图片
    public String id;//ID

    //    public  imagesArray;//图片数组
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.broke_fragment_list_view, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        list_item = (ListView) view.findViewById(R.id.list_item);
        //获取爆料列表
        RequestParams params = new RequestParams("http://tr.zzapi.gson.cn/api/news/list?type=FACT&page=1&rows=50");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                com.alibaba.fastjson.JSONObject json = JSON.parseObject(s);
                data = json.getJSONArray("data");
                //创建适配器
                class DiscloseAdapter extends BaseAdapter {
                    public DiscloseAdapter() {
                        list = new ArrayList<>();
                        for (int i = 0; i < data.size(); i++) {
                            JSONObject obj1 = data.getJSONObject(i);
                            title = obj1.getString("title");//标题
                            summary = obj1.getString("summary");//摘要
                            dateline = obj1.getString("dateline");//爆料时间
                            good = obj1.getString("good");
                            id = obj1.getString("id");
                            bad = obj1.getString("bad");
                            commentNum = obj1.getString("commentNum");
                            images = obj1.getString("images");//所有图片
                            user = obj1.getString("user");//用户
                            JSONObject obj2 = JSON.parseObject(user);
                            avatar = obj2.getString("avatar");//头像
                            nickName = obj2.getString("nickName");//昵称
                            Map<String, Object> map = new HashMap<>();
                            map.put("title", title);
                            map.put("summary", summary);
                            map.put("dateline", dateline);
                            map.put("summary", summary);
                            map.put("good", good);
                            map.put("bad", bad);
                            map.put("commentNum", commentNum);
                            map.put("images", images);
                            map.put("avatar", avatar);
                            map.put("nickName", nickName);
                            map.put("id", id);
                            list.add(map);
                        }
                    }

                    public int getCount() {
                        return list.size();
                    }

                    @Override
                    public Object getItem(int position) {
                        return list.get(position);
                    }

                    @Override
                    public long getItemId(int position) {
                        return position;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        final LayoutTextView5 layoutTextView5;
                        if (convertView == null) {
                            convertView = LayoutInflater.from(getActivity()).inflate(R.layout.broke_fragment_whole_adapter, null);
                            layoutTextView5 = new LayoutTextView5();
                            layoutTextView5.tv_title = (TextView) convertView.findViewById(R.id.title);
                            layoutTextView5.tv_summary = (TextView) convertView.findViewById(R.id.summary);
                            layoutTextView5.tv_nickName = (TextView) convertView.findViewById(R.id.nickName);
                            layoutTextView5.tv_dateline = (TextView) convertView.findViewById(R.id.dateline);
                            layoutTextView5.tv_good = (TextView) convertView.findViewById(R.id.good);
                            layoutTextView5.tv_bad = (TextView) convertView.findViewById(R.id.bad);
                            layoutTextView5.tv_commentNum = (TextView) convertView.findViewById(R.id.commentNum);
                            layoutTextView5.iv_avatar = (ImageView) convertView.findViewById(R.id.avatar);
                            layoutTextView5.linear_images = (FrameLayout) convertView.findViewById(R.id.images);
                            layoutTextView5.iv_img1 = (ImageView) convertView.findViewById(R.id.img1);
                            layoutTextView5.tv_pictureNumber = (TextView) convertView.findViewById(R.id.pictureNumber);
                            convertView.setTag(layoutTextView5);
                        } else {
                            layoutTextView5 = (LayoutTextView5) convertView.getTag();
                        }
                        layoutTextView5.tv_title.setText((String) list.get(position).get("title"));//标题
                        layoutTextView5.tv_summary.setText((String) list.get(position).get("summary"));//摘要
                        layoutTextView5.tv_nickName.setText((String) list.get(position).get("nickName"));//昵称
                        layoutTextView5.tv_dateline.setText((String) list.get(position).get("dateline"));//爆料时间
                        layoutTextView5.tv_good.setText((String) list.get(position).get("good"));//赞
                        layoutTextView5.tv_bad.setText((String) list.get(position).get("bad"));//踩
                        layoutTextView5.tv_commentNum.setText((String) list.get(position).get("commentNum"));//评论数
                        //设置头像
                        ImageOptions options = new ImageOptions.Builder()
                                .setCircular(true)//设置圆形
                                .setLoadingDrawableId(R.mipmap.default_avatar)//加载过程中的等待图片
                                .setFailureDrawableId(R.mipmap.default_avatar)//加载出错显示的图片
                                .build();
                        //显示远程图片
                        x.image().bind(
                                layoutTextView5.iv_avatar,
                                "http://tr.zzapi.gson.cn/" + list.get(position).get("avatar"),
                                options
                        );
                        //爆料图片
                        //如果没有图片，则隐藏存放图片的LinearLayout
                        String img = list.get(position).get("images") + "";
                        String[] imagesArray = img.split(",");//获取的数据已逗号为分隔符拼成一个数组
                        layoutTextView5.tv_pictureNumber.setText(imagesArray.length + "");
                        if (img == null || img.length() == 0) {
                            layoutTextView5.linear_images.setVisibility(View.GONE);
                        } else {
                            layoutTextView5.linear_images.setVisibility(View.VISIBLE);
                            ImageOptions options1 = new ImageOptions.Builder().build();
                            x.image().bind(layoutTextView5.iv_img1, "http://tr.zzapi.gson.cn/" + imagesArray[0], options1);
                            /*String[] imagesArray = img.split(",");//获取的数据已逗号为分隔符拼成一个数组
                            //设置图片
                            if (imagesArray.length == 1){
                                ImageOptions options1 = new ImageOptions.Builder().build();
                                x.image().bind(layoutTextView5.iv_img1, "http://tr.zzapi.gson.cn/" + imagesArray[0], options1);
                            }else if (imagesArray.length == 2){
                                ImageOptions options2 = new ImageOptions.Builder().build();
                                x.image().bind(layoutTextView5.iv_img1, "http://tr.zzapi.gson.cn/" + imagesArray[0], options2);
                                x.image().bind(layoutTextView5.iv_img2, "http://tr.zzapi.gson.cn/" + imagesArray[1], options2);
                            }else if (imagesArray.length >= 3){
                                ImageOptions options3 = new ImageOptions.Builder().build();
                                x.image().bind(layoutTextView5.iv_img1, "http://tr.zzapi.gson.cn/" + imagesArray[0], options3);
                                x.image().bind(layoutTextView5.iv_img2, "http://tr.zzapi.gson.cn/" + imagesArray[1], options3);
                                x.image().bind(layoutTextView5.iv_img3, "http://tr.zzapi.gson.cn/" + imagesArray[2], options3);
                            }*/
                        }
                        return convertView;
                    }
                }
                //添加适配器
                BaseAdapter a = new DiscloseAdapter();
                list_item.setAdapter(a);
                //查看爆料详情
                list_item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        BaseAdapter hvla = (BaseAdapter) parent.getAdapter();
                        //从列表项获取数据
                        Map<String, String> m = (Map<String, String>) hvla.getItem(position);
                        Intent intent = new Intent(getActivity(), BrokeDetailsA.class);
                        intent.putExtra("id", m.get("id"));
                        startActivity(intent);
                    }
                });
            }

            //优化适配器
            class LayoutTextView5 {
                public TextView tv_title;
                public TextView tv_summary;
                public ImageView iv_avatar;
                public TextView tv_nickName;
                public TextView tv_dateline;
                public TextView tv_good;
                public TextView tv_bad;
                public TextView tv_commentNum;
                public FrameLayout linear_images;//存放图片的线性布局
                public ImageView iv_img1;
                public TextView tv_pictureNumber;
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
